package com.servlet; /**
 * @author wzm
 * @create 2023-03-14 11:02
 */

import com.entity.User;
import com.service.UserSerivce;
import com.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {

    private UserSerivce userSerivce = new UserServiceImpl();

    //登录
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //获取表单参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String remember = request.getParameter("remember");

        User login = userSerivce.login(new User(username, password));
        if(login != null){
            //成功
            //判断是否勾选记住
            if ("1".equals(remember)) {//勾选
                //将信息存储到Cookie中
                Cookie uname = new Cookie("username",username);
                Cookie pwd = new Cookie("password",password);

                //存储7天
                uname.setMaxAge(60*60*24*7);
                pwd.setMaxAge(60*60*24*7);

                //默认情况下Cookie仅存储在当前路径发送到服务器
                uname.setPath(request.getContextPath()); //当前项目下路径存储
                pwd.setPath(request.getContextPath());

                response.addCookie(uname);
                response.addCookie(pwd);
            }

            //登录成功
            //将登录的用户存入Session方便在会话中使用Session
            request.getSession().setAttribute("loginUser",login);
            response.sendRedirect(request.getContextPath()+"/index.jsp");


        }else{
            //失败
            request.setAttribute("errorMsg","用户名或密码错误，请重试！");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }

    }

    //注册
    protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("注册");
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("checkCode");//验证码

        //将session中验证码和用户输入的验证码进行对比
        String sessionCode = (String) request.getSession().getAttribute("sessionCode");
        if(!sessionCode.equalsIgnoreCase(code)){
            //对比失败
            request.setAttribute("msg","验证码错误");
            request.getRequestDispatcher("/register.jsp").forward(request,response);
            return;//停止注册
        }

        boolean register = userSerivce.register(new User(username, password));

        if(register){
            //注册成功
          //  request.setAttribute("login_msg", "注册成功,请登录");
          //  request.getRequestDispatcher("/login.jsp").forward(request,response);

            //防止重复提交
            request.getSession().setAttribute("login_msg", "注册成功,请登录");
            response.sendRedirect(request.getContextPath()+"/login.jsp");

        }else{
            //注册失败
            request.setAttribute("msg","内部注册发生错误");
            request.getRequestDispatcher("/register.jsp").forward(request,response);

        }


    }

    //查询

    //修改密码

}
